import React, { Component } from 'react'
import './Item.css'

export default class Item extends Component {
  state = {
    mouse: false,
  }
  //鼠标移入、移出的回调
  handleMouse = (flag) => {
    return () => {
      this.setState({ mouse: flag })
    }
  }
  handleCheck = (id) => {
    return (e) => {
      // console.log(id, e.target.checked)
      this.props.checkTodo(id, e.target.checked)
    }
  }

  render() {
    let { item } = this.props
    const { mouse } = this.state
    return (
      <li
        style={{ backgroundColor: mouse ? '#ddd' : 'white' }}
        onMouseEnter={this.handleMouse(true)}
        onMouseLeave={this.handleMouse(false)}
      >
        <label>
          <input
            type="checkbox"
            checked={item.done}
            onChange={this.handleCheck(item.id)}
          />
          <span>{item.name}</span>
        </label>
        <button
          className="btn btn-danger"
          onClick={() => this.deleteTodoInItem(item.id)}
          style={{ display: mouse ? 'block' : 'none' }}
        >
          删除
        </button>
      </li>
    )
  }
  deleteTodoInItem = (id) => {
    // console.log(id)
    this.props.deleteTodo(id)
  }
}
